package com.irdstudio.efp.esb.service.impl.frontsystem;

import com.alibaba.fastjson.JSONObject;
import com.irdstudio.basic.framework.core.constant.ESBConstant;
import com.irdstudio.basic.framework.core.exception.ESBException;
import com.irdstudio.efp.esb.common.client.resp.EsbRespServiceBean;
import com.irdstudio.efp.esb.common.constant.ESBClientConstance;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.service.bo.req.frontsystem.NetworkBankInfoReqBean;
import com.irdstudio.efp.esb.service.bo.resp.frontsystem.NetworkBankInfoRespBean;
import com.irdstudio.efp.esb.service.client.ESBRequestBuilder;
import com.irdstudio.efp.esb.service.client.EsbBaseBean;
import com.irdstudio.efp.esb.service.facade.frontsystem.NetworkBankInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service("networkBankInfoService")
public class NetworkBankInfoServiceImpl implements NetworkBankInfoService {
    private static Logger logger = LoggerFactory.getLogger(NetworkBankInfoServiceImpl.class);

    @Override
    public NetworkBankInfoRespBean applyNetworkBankInfo(NetworkBankInfoReqBean networkBankInfoReqBean) throws ESBException {
        logger.info("======>个人网银客户详细信息查询接口【" + networkBankInfoReqBean.getGlobaNo() + "_20130001_14】开始<======");
        NetworkBankInfoRespBean networkBankInfoRespBean = null;
        try {
            EsbBaseBean esbBaseBean = EsbBaseBean.EsbBaseBeanBuilder.
                    anEsbBaseBean().
                    withINSTID(ESBClientConstance.bdInstId).withGLBLSRLNO(networkBankInfoReqBean.getGlobaNo()).build();
            EsbRespServiceBean recvBean = ESBRequestBuilder.buildClient().
                    withEsbBaseBean(esbBaseBean)
                    .withBody(networkBankInfoReqBean)
                    .withTradeNo(ESBConstant.TRADE_CODE_20130001)
                    .withScene(ESBConstant.SCENE_NO_14)
                    .create()
                    .sendAndReceive();
            if (EsbBizEnums.RetCodeEnum.SUCCESS.VALUE.equals(recvBean.getSysHead().getRetInfArry().get(0).getRetCd())) {
                networkBankInfoRespBean = recvBean.getBody(NetworkBankInfoRespBean.class);
            } else {
                throw new ESBException(recvBean.getSysHead().getRetInfArry().get(0).getRetMsg());
            }
            logger.info("个人网银客户详细信息查询接口【20130001_14】结束，返回信息：" + JSONObject.toJSONString(networkBankInfoRespBean));
        } catch (Exception e) {
            logger.error("个人网银客户详细信息查询接口【20130001_14】出现异常：" + e.getMessage());
            throw new ESBException("个人网银客户详细信息查询接口【20130001_14】出现异常：" + e.getMessage());
        } finally {
            logger.info("======>个人网银客户详细信息查询接口【" + networkBankInfoReqBean.getGlobaNo() + "_20130001_14】结束<======");
        }
        return networkBankInfoRespBean;
    }
}